package com.itheima.kafka.boot.controller;

import com.alibaba.fastjson.JSON;
import com.itheima.kafka.boot.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Version: V1.0
 */
@RestController
@RequestMapping("kafka")
public class HelloController {

    @Autowired
    KafkaTemplate kafkaTemplate;

    @GetMapping("hello")
    public String hello() {
        // 参数1：发送主题 目的地
        // 参数2：发送数据
//        kafkaTemplate.send("kafka-hello", "Hello Kafka!");

        User user = User.builder().username("张三").age(20).build();
        // json字符串
        String userJson = JSON.toJSONString(user);
        kafkaTemplate.send("kafka-hello", userJson);
        return "OK";
    }

}
